﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Reflection;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace SinacorWebTools.Web
{
    public class BasePage : System.Web.UI.Page
    {
        public SortDirection SortOrder
        {
            get
            {
                if (!object.ReferenceEquals(ViewState["sortDirection"], null))
                    return (SortDirection)ViewState["sortDirection"];
                else
                    return SortDirection.Ascending;
            }
            set
            {
                ViewState["sortDirection"] = value;
            }
        }

        public string SortExpression
        {
            get { return ViewState["sortExpression"].ToString(); }
            set { ViewState["sortExpression"] = value; }
        }

        public void ConfigGridViewHeader(GridView gridView, GridViewRow itemRow)
        {
            string sortExpression = string.Empty;
            TableCell tableCell;
            for (int i = 0; i < gridView.Columns.Count; i++)
            {
                sortExpression = gridView.Columns[i].SortExpression;
                tableCell = itemRow.Cells[i];

                if (!string.IsNullOrEmpty(sortExpression))
                {
                    foreach (Control gridViewRowCellControl in itemRow.Cells[i].Controls)
                    {
                        LinkButton linkButton = gridViewRowCellControl as LinkButton;
                        if ((linkButton != null) && (linkButton.CommandName == "Sort"))
                        {
                            tableCell.Style.Add(HtmlTextWriterStyle.Cursor, "hand");
                            tableCell.Style.Add(HtmlTextWriterStyle.Cursor, "pointer");
                            break;
                        }
                    }
                }
            }

            AddGridViewSortDirectionImage(gridView, itemRow);
        }

        public void AddGridViewSortDirectionImage(GridView gridView, GridViewRow itemRow)
        {
            if (gridView.AllowSorting == false)
                return;

            Image sortImage = new Image();
            sortImage.ImageUrl = (this.SortOrder == SortDirection.Ascending ? @"~/images/arrowup.gif" : @"~/images/arrowdown.gif");
            sortImage.Visible = true;

            string colExpr = string.Empty;
            for (int i = 0; i < gridView.Columns.Count; i++)
            {
                colExpr = gridView.Columns[i].SortExpression;
                if (colExpr != "" && colExpr == this.SortExpression)
                {
                    LinkButton lnk = itemRow.Cells[i].Controls[0] as LinkButton;
                    lnk.BackColor = gridView.HeaderStyle.BackColor;
                    lnk.Font.Bold = gridView.HeaderStyle.Font.Bold;
                    lnk.CssClass = "ColHeader";

                    if ((lnk != null) && (lnk.CommandName == "Sort"))
                    {
                        itemRow.Cells[i].Controls.Clear();

                        TableCell tbcText = new TableCell();
                        tbcText.Controls.Add(lnk);

                        TableCell tbcImage = new TableCell();
                        tbcImage.Controls.Add(sortImage);

                        TableRow tbr = new TableRow();
                        tbr.Cells.AddRange(new TableCell[] { tbcText, tbcImage });

                        Table tb = new Table();
                        tb.BorderWidth = 0;
                        tb.Width = 0;
                        tb.Rows.Add(tbr);

                        itemRow.Cells[i].Controls.Add(tb);
                    }
                }
            }
        }

        public static DataTable ConvertListToDataTable<T>(List<T> list)
        {
            DataTable dt = new DataTable();

            foreach (PropertyInfo info in typeof(T).GetProperties())
                dt.Columns.Add(new DataColumn(info.Name, info.PropertyType));

            foreach (T t in list)
            {
                DataRow row = dt.NewRow();
                foreach (PropertyInfo info in typeof(T).GetProperties())
                    row[info.Name] = info.GetValue(t, null);
                dt.Rows.Add(row);
            }
            return dt;
        }
    }
}
